<template>
	<view>
		<view class="content-item">
			<view class="user-info">
				<view class="avatar">
					<image src="/static/user.png"></image>
				</view>
				<view class="name">
					<text>{{userInfo.mname}}</text>
				</view>
			</view>
		</view>
		<uni-list>
			<uni-list-item showExtraIcon title="手机号码" :extraIcon="{color: '#999999',size: '22',type: 'phone'}" :right-text="userInfo.phone"/>
			<uni-list-item showExtraIcon title="角色名称" :extraIcon="{color: '#999999',size: '22',type: 'compose'}" :right-text="userInfo.mname"/>
			<uni-list-item v-if="userType == '1'" showExtraIcon showArrow clickable title="实名信息" :extraIcon="{color: '#999999',size: '22',type: 'auth'}" :right-text="userInfo.name || '未设置'" @click="showModal"/>
			<uni-list-item v-if="userType == '1'" showExtraIcon showArrow clickable title="收款账号" :extraIcon="{color: '#999999',size: '22',type: 'wallet-filled'}" :right-text="userInfo.identity || '未设置'" @click="showModal"/>
			<uni-list-item showExtraIcon showArrow clickable title="退出登录" :extraIcon="{color: '#999999',size: '22',type: 'upload'}" @click="goOut"/>
		</uni-list>
		<uni-popup ref="alipayModal">
			<view class="modal-body">
				<view class="modal-header">
					<uni-icons type="close" color="#ff5500" size="26" @click="closeModal"></uni-icons>
				</view>
				<view class="modal-form">
					<uni-forms :modelValue="formData" label-position="top" label-width="150">
						<uni-forms-item label="支付宝姓名" name="name">
							<uni-easyinput v-model="formData.name" placeholder="请输入账户真实姓名"></uni-easyinput>
						</uni-forms-item>
						<uni-forms-item label="支付宝账号" name="identity">
							<uni-easyinput v-model="formData.identity" placeholder="请输入支付宝账号"></uni-easyinput>
						</uni-forms-item>
					</uni-forms>
					<button class="button" type="primary" size="mini" @click="submit">提交</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				userInfo: {},
				userType: "",
				formData: {}
			}
		},
		
		onShow() {
			let uid = uni.getStorageSync('uid');
			if(!uid) {
				uni.reLaunch({
					url:'/pages/user/login'
				})
			}
			
			this.userType = uni.getStorageSync('userType');
			
			this.getUserInfo()
		},
		 
		methods: {
			getUserInfo() {
				uni.request({
					url: getApp().globalData.apiUrl + '/card/api/v1/queryTransferAccountByUserId',
					data: {
						userId: uni.getStorageSync('uid')
					},
					success: res => {
						if(res.data.success) {
							this.userInfo = {
								phone: uni.getStorageSync('phone'),
								mname: uni.getStorageSync('mname'),
								...res.data.result
							}
							
							if(res.data.result) {
								this.formData = {
									name: res.data.result.name,
									identity: res.data.result.identity
								}
							}
						} else {
							uni.showToast({
								icon:'error',
								title: res.data.message
							})
						}
					}
				})
			},
			
			showModal() {
				this.$refs.alipayModal.open()
			},
			
			closeModal() {
				this.formData = {}
				this.$refs.alipayModal.close()
			},
			
			submit() {
				uni.showLoading({
					title:'请求中...'
				})
				
				uni.request({
					url: getApp().globalData.apiUrl + '/card/api/v1/setTransferAccount',
					method: "PUT",
					data: {
						userId: uni.getStorageSync('uid'),
						identity: this.formData.identity,
						name: this.formData.name
					},
					success: res => {
						if(res.data.success) {
							uni.showToast({
								icon:'success',
								title: '设置成功'
							})
							this.closeModal()
							this.getUserInfo()
						} else {
							uni.showToast({
								icon:'error',
								title: res.data.message
							})
						}
					}
				})
			},
			
			goOut() {
				uni.clearStorage()
				uni.reLaunch({
					url: '/pages/user/login'
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.content-item {
		height: 300rpx;
		background-color: #00aaff;
		color: #fff;
	}
	
	.user-info{
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		height: 100%;
		
		.avatar {
			background-color: #fff;
			border-radius: 100%;
			height: 120rpx;
			width: 120rpx;
			
			image {
				width: 100%;
				height: 100%;
			}
		}	
				
		.name{
			margin-top: 30rpx;
		}
	}

	.modal-body {
		background: #fff;
		border-radius: 20rpx;
		width: 600rpx;
		padding: 10rpx;
		display: flex;
		flex-direction: column;
		
		.modal-header {
			display: flex;
			justify-content: flex-end;
		}
		
		.modal-form {
			padding: 20rpx;
			
			.button {
				width: 100%;
				height: 80rpx;
				line-height: 80rpx;
				font-size: 14px;
			}
		}
	}
</style>